<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>随机整数处理</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            line-height: 1.6;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
        }
        h1 {
            text-align: center;
            color: #333;
        }
        .button-group {
            margin: 20px 0;
            text-align: center;
        }
        button {
            border-radius: 10px;
            margin: 2px 5px;
            padding: 5px 8px;
            cursor: pointer;
            background-color: #f0f0f0;
            border: 1px solid #ccc;
        }
        button:hover {
            background-color: #e0e0e0;
        }
        textarea {
            width: 100%;
            height: 200px;
            margin: 10px 0;
            padding: 10px;
            box-sizing: border-box;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>随机产生20个4位整数并从小到大进行排序</h1>

        <textarea id="output" readonly></textarea>

        <div class="button-group">
            <button id="generateBtn">随机产生20个整数</button>
            <button id="findDivisibleBtn">找出能被5整除的整数</button>
            <button id="resetBtn">重置</button>
        </div>
    </div>

    <script>
        // 存储随机生成的数组
        let numbers = [];

        // 获取DOM元素
        const output = document.getElementById('output');
        const generateBtn = document.getElementById('generateBtn');
        const findDivisibleBtn = document.getElementById('findDivisibleBtn');
        const resetBtn = document.getElementById('resetBtn');

        // 生成随机数按钮点击事件
        generateBtn.addEventListener('click', function() {
            // 清空数组
            numbers = [];

            // 生成20个1000-9999的随机整数
            for (let i = 0; i < 20; i++) {
                numbers.push(Math.floor(Math.random() * 9000) + 1000);
            }

            // 显示原始数组
            output.value = "随机产生20个4位整数，分别如下：\n";
            output.value += numbers.join(', ') + "\n\n";

            // 排序数组
            const sortedNumbers = [...numbers].sort((a, b) => a - b);

            // 显示排序结果
            output.value += "从小到大排序后结果为：\n";
            output.value += sortedNumbers.join(', ');
        });

        // 找出能被5整除的整数按钮点击事件
        findDivisibleBtn.addEventListener('click', function() {
            if (numbers.length === 0) {
                output.value = "请先生成20个随机整数！";
                return;
            }

            // 找出能被5整除的数
            const divisibleBy5 = numbers.filter(num => num % 5 === 0);

            // 添加到输出
            if (divisibleBy5.length > 0) {
                output.value += "\n\n能被5整除的整数有：\n";
                output.value += divisibleBy5.join(', ');
            } else {
                output.value += "\n\n没有能被5整除的整数。";
            }
        });

        // 重置按钮点击事件
        resetBtn.addEventListener('click', function() {
            output.value = "";
            numbers = [];
        });
    </script>
</body>
</html>